package TestCases;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SQLUtil {

    private static final Pattern COMMAND_PATTERN = Pattern.compile("^(\\w+)");

    // 解析SQL语句并返回操作类型
    private static String parseSQLType(String sql) {
        sql = sql.trim().toUpperCase();  // 转换为大写以便比较
        Matcher matcher = COMMAND_PATTERN.matcher(sql);

        if (matcher.find()) {
            String command = matcher.group(1);

            switch (command) {
                case "CREATE":
                    return "CREATE";
                case "SHOW":
                    return "SHOW";
                case "DELETE":
                    return "DELETE";
                case "DROP":
                    return "DROP";
                case "SELECT":
                    return "SELECT";
                case "USE":
                    return "USE";
                case "EXIT":
                    return "EXIT";
                case "ALTER":
                    return "ALTER";
                case "GRANT":
                    return "GRANT";
                case "FLUSH":
                    return "FLUSH";
                case "DESC":
                    return "DESC";
                case "RENAME":
                    return "RENAME";
                case "INSERT":
                    return "INSERT";
                case "UPDATE":
                    return "UPDATE";
                case "CALL":
                    return "CALL";
                case "WAIT":
                    return "WAIT";
                case "WAITFOR":
                    return "WAITFOR";
                case "UNION":
                    return "UNION";
                case "SET":
                    return "SET";
                default:
                    return "Unknown Operation";
            }
        }

        return "Unknown Operation";
    }



    // 存储解析到的sql操作类型
    public static List<String> generateSQLType(List<String> sqlStatements) {
        List<String> sqlType = new ArrayList<>();;
        for (String sql : sqlStatements) {
            String operationType = parseSQLType(sql);
            sqlType.add(operationType);
        }
        return sqlType;
    }

}
